package com.hedl.practice.diyJava.jsonAndXml.dom;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;

public class Study_Dom {

    private static void printNode(Node n,int indent){
        for (int i = 0;i < indent;i++){
            System.out.print(' ');
        }
        switch (n.getNodeType()){
            case Node.DOCUMENT_NODE:        // Document节点
                System.out.println("Document: "+n.getNodeName());
                break;
            case Node.ELEMENT_NODE:         // 元素节点
                System.out.println("Element: "+n.getNodeName());
                break;
            case Node.TEXT_NODE:            // 文本
                System.out.println("Text: "+n.getNodeName()+" = "+n.getNodeValue());
                break;
            case Node.ATTRIBUTE_NODE:       // 属性
                System.out.println("Attr: "+n.getNodeName()+" = "+n.getNodeValue());
                break;
            case Node.CDATA_SECTION_NODE:
                System.out.println("CDATA: "+n.getNodeName()+" = "+n.getNodeValue());
                break;
            case Node.COMMENT_NODE:
                System.out.println("Comment: "+n.getNodeName()+" = "+n.getNodeValue());
                break;
            default:        // 其他
                System.out.println("NodeType: "+n.getNodeType()+", NodeName: "+n.getNodeName());
        }
        for (Node child = n.getFirstChild();child != null;child = child.getNextSibling()){
            printNode(child,indent + 1);
        }
    }


    public static void main(String[] args) throws Exception {
        InputStream input = Study_Dom.class.getResourceAsStream("/book.xml");
        // 解析并获取Document对象:
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(input);
        printNode(doc,0);
    }
}
